/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hotgammon.domain.game.winner;

import hotgammon.domain.board.*;

/**
 *
 * @author truck
 */
public class DeltamonWinnerStrategy implements WinnerStrategy{
    private boolean winnerIsSet;
    private Color winner;
    
    public DeltamonWinnerStrategy(){
        winnerIsSet = false;
        winner = Color.NONE;
    }
    public Color getWinner(Board board) {
        if(!winnerIsSet && isBlackWinner(board)){
            winner = Color.BLACK;
            winnerIsSet = true;
        }
        if(!winnerIsSet && isRedWinner(board)){
            winner = Color.RED;
            winnerIsSet = true;
        }
            
        return winner;
    }

    private boolean isBlackWinner(Board b){
        int count = 0;
        if(Color.BLACK == b.getColor(Location.B1)){
            count += b.getCount(Location.B1);
        }
        if(Color.BLACK == b.getColor(Location.B2)){
            count += b.getCount(Location.B2);
        }
        if(Color.BLACK == b.getColor(Location.B3)){
            count += b.getCount(Location.B3);
        }
        if(Color.BLACK == b.getColor(Location.B4)){
            count += b.getCount(Location.B4);
        }
       if(Color.BLACK == b.getColor(Location.B5)){
            count += b.getCount(Location.B5);
        }
        if(Color.BLACK == b.getColor(Location.B6)){
            count += b.getCount(Location.B6);
        }
        
        if(count == 15) return true;
        return false;
    }
    
    private boolean isRedWinner(Board b){
        int count = 0;
        if(Color.RED == b.getColor(Location.R1)){
            count += b.getCount(Location.R1);
        }
        if(Color.RED == b.getColor(Location.R2)){
            count += b.getCount(Location.R2);
        }
        if(Color.RED == b.getColor(Location.R3)){
            count += b.getCount(Location.R3);
        }
        if(Color.RED == b.getColor(Location.R4)){
            count += b.getCount(Location.R4);
        }
       if(Color.RED == b.getColor(Location.R5)){
            count += b.getCount(Location.R5);
        }
        if(Color.RED == b.getColor(Location.R6)){
            count += b.getCount(Location.R6);
        }
        
        if(count == 15) return true;
        return false;
    }
}
